/**
 * Created by zhouli on 18/4/25
 * Email li.zhou@huilianyi.com
 */
import React from 'react';
import {connect} from 'react-redux';
import {Table, Badge, Button, Popover, message} from 'antd';
import SearchArea from 'components/search-area';
import 'styles/setting/operation-log/operation-log.scss';
import {messages} from "share/common";
import OLService from 'containers/setting/operation-log/operation-log.service';
import moment from 'moment';
import ExportModal from 'components/template/export-modal/export-modal';
import ResizeTable from "components/resize-table";

class OperationLog extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      loading: false,
      data: [],
      columns: [
        {
          // 时间
          title: messages('setting.key810'/*时间*/),
          dataIndex: 'operationTime',
          width: '20%'
        },

        {
          // 管理员
          title: messages('setting.key811'/*操作者*/),
          dataIndex: 'username',
          width: '15%'
        },

        {
          // 操作对象
          title: messages('setting.key812'/*操作对象*/),
          dataIndex: 'entityType',
          width: '15%'
        },
        {
          // 操作类型
          title: messages('setting.key813'/*操作类型*/),
          dataIndex: 'operationType',
          width: '20%'
        },
        {
          // 操作明细
          title: messages('setting.key814'/*操作明细*/),
          dataIndex: 'operationTitle',
          render: operationTitle => (
            <Popover content={operationTitle}>
              {operationTitle}
            </Popover>)
        },
      ],
      pagination: {
        total: 0,
        page: 0,
        pageSize: 10,
      },
      searchForm: [
        //时间从
        {
          type: 'date',
          id: 'operationTimeFrom',
          label: messages('setting.key815'/*时间从*/)
        },
        //时间到
        {
          type: 'date',
          id: 'operationTimeTo',
          label: messages('setting.key816'/*时间到*/)
        },
        //操作对象
        {
          type: 'select',
          id: 'entityType',
          label: messages('setting.key812'/*操作对象*/),
          options: [],
          defaultValue: "",
          labelKey: 'setOfBooksCode',
          valueKey: 'id',
        },
        //管理员
        {
          type: 'list',
          id: 'userOids',
          label: messages('setting.key811'/*操作者*/),
          listType: 'user',
          labelKey: 'fullName',
          valueKey: 'userOID',
          placeholder: messages("common.please.select"),//"请选择",
        },

      ],
      searchParams: {
        operationTimeFrom: '',
        operationTimeTo: '',
        entityType: '',
        userOids: [],//这个是操作人，直接是选整个集团下面的人
      },
    };
  }

  componentDidMount() {
    this.getList();
    //给一个初始值
    const {searchForm} = this.state;
    //下面针对操作对象下拉单进行处理
    let entityTypeSelecter = searchForm[2];
    this.getSystemValueList(2029)
      .then((res) => {
        let options = res.data.values;
        options.map((item, index) => {
          item.label = item.messageKey;
        })
        entityTypeSelecter.options = options;
        this.setState({
          searchForm,
        })
      })
  }

  //得到列表数据
  getList() {
    this.setState({loading: true});
    const {searchParams, pagination} = this.state;
    searchParams.page = pagination.page;
    searchParams.size = pagination.pageSize;
    OLService.getOperationLogList(searchParams)
      .then((response) => {
        pagination.total = Number(response.headers['x-total-count']) ? Number(response.headers['x-total-count']) : 0;
        pagination.current = searchParams.page + 1;
        this.setState({
          data: response.data,
          loading: false,
          pagination
        })
      })
      .catch((res) => {
        this.setState({loading: false});
      });
  }

  //分页点击
  onChangePager = (pagination, filters, sorter) => {
    this.setState({
      pagination: {
        page: pagination.current - 1,
        pageSize: pagination.pageSize
      }
    }, () => {
      this.getList();
    })
  };


  search = (result) => {
    //如果选择了起始时间
    if (result.operationTimeFrom) {
      if (!result.operationTimeTo) {
        //请选择结束时间
        message.warn(messages('setting.key817'/*请选择结束时间*/));
        return;
      }
    }
    if (result.operationTimeTo) {
      if (!result.operationTimeFrom) {
        //请选择开始时间
        message.warn(messages('setting.key818'/*请选择开始时间*/));
        return;
      }
    }

    //时间先后校验
    if(result.operationTimeFrom && result.operationTimeTo){
      if(moment(result.operationTimeTo).format("YYYY-MM-DD") < moment(result.operationTimeFrom).format("YYYY-MM-DD")){
        //结束时间须大于起始时间
        message.warn(messages('setting.key819'/*结束时间须大于起始时间*/));
        return;
      }
    }
    this.setState({
      pagination: {
        total: 0,
        page: 0,
        pageSize: 10,
      },
      searchParams: {
        operationTimeFrom: result.operationTimeFrom ? moment(result.operationTimeFrom).format("YYYY-MM-DD") : '',
        operationTimeTo: result.operationTimeTo ? moment(result.operationTimeTo).format("YYYY-MM-DD") : '',
        userOids: result.userOids ? result.userOids : [],
        entityType: result.entityType ? result.entityType : '',
      }
    }, () => {
      this.getList();
    })
  };

  clear = () => {
    this.setState({
      searchParams: {
        operationTimeFrom: '',
        operationTimeTo: '',
        userOids: [],
        entityType: ''
      }
    }, () => {
      this.getList();
    })
  };


  render() {
    const {columns, data, loading, pagination, searchForm,searchParams} = this.state;
    return (
      <div className="operation-log">
        <SearchArea
          searchForm={searchForm}
          submitHandle={this.search}
          clearHandle={this.clear}
        />
        <div className="table-header">
            <ExportModal
              type="btn"
              btnType="primary"
              codeCheck={true}
              exportTitle={messages('setting.key1745')/*导出*/}
              exportType="DATA_OPERATION"
              exportCondition={{
                dataOperationSearchCommand:{
                  entityType:searchParams.entityType,
                  operationTimeFrom:searchParams.operationTimeFrom,
                  operationTimeTo:searchParams.operationTimeTo,
                  userOIDs:searchParams.userOids,
                }
              }}
              exportCommand={"DATA_OPERATION"}/>
          <div className="table-header-title">
            {messages("common.total", {total: pagination.total ? pagination.total : '0'})}
          </div>
          {/* 共total条数据 */}

        </div>
        <ResizeTable
          columns={columns}
          dataSource={data}
          pagination={pagination}
          loading={loading}
          onChange={this.onChangePager}
          bordered
          size="middle"
        />
      </div>
    )
  }

}

OperationLog.contextTypes = {
  router: React.PropTypes.object
};

function mapStateToProps() {
  return {}
}

export default connect(mapStateToProps)(OperationLog);
